/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.documents.service.country;

import com.documents.common.core.PageBean;
import com.documents.common.core.service.BaseSrv;
import com.documents.htmlobj.CountryObj;
import com.documents.pojo.Country;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class CountryService extends BaseSrv<Country, Integer> {

    public List<CountryObj> createList() {

        List<Country> root = this.find("temp4=?", new Object[]{1}, "id");
        List<CountryObj> l = new ArrayList<CountryObj>();
        if (root != null) {

            for (Country p : root) {
                CountryObj o = new CountryObj();
                o.setId(p.getId());
                o.setLevel(1);
                o.setName(p.getName());
                List<Country> level2 = this.find("temp4=?", new Object[]{o.getId()}, "id");
                if (level2 != null && level2.size() != 0) {
                    List<CountryObj> l2 = new ArrayList<CountryObj>();
                    for (Country p2 : level2) {
                        CountryObj o2 = new CountryObj();
                        o2.setId(p2.getId());
                        o2.setLevel(2);
                        o2.setName(p2.getName());
                        List<Country> level3 = this.find("temp4=?", new Object[]{o2.getId()}, "id");
                        if (level3 != null && level3.size() != 0) {
                            List<CountryObj> l3 = new ArrayList<CountryObj>();
                            for (Country p3 : level3) {

                                CountryObj o3 = new CountryObj();
                                o3.setId(p3.getId());
                                o3.setLevel(3);
                                o3.setName(p3.getName());
                                l3.add(o3);
                            }
                            o2.setList(l3);
                            l2.add(o2);
                        } else {
                            l2.add(o2);
                        }

                    }
                    o.setList(l2);
                    l.add(o);
                } else {

                    l.add(o);
                    continue;
                }

            }
        }

        return l;
    }
    
    
      public PageBean getPageBean(int page, int pageSize, Country country) {
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);
        pageBean.setCurrentPage(page);
        String condition = "";
        Object[] parameters = new Object[1];
        int paramIndex = 0;
        if (country != null) {
            int pid = country.getTemp4();
            if (pid >= 0) {
                condition = condition + "temp4=?";
                parameters[paramIndex] = new Integer(pid);
                paramIndex++;
            }
            Long allRow = this.findTotalCount(condition, parameters);
            int totalPage = PageBean.countTotalPage(pageSize, allRow.intValue()); // 总页数
            pageBean.setAllRow(allRow.intValue());
            pageBean.setTotalPage(totalPage);
            List<Country> list;
             if (page == 1) {
                list = this.findWithPaging(condition, parameters, "code", page-1, pageSize);
            } else {
                list = this.findWithPaging(condition, parameters, "code", (page - 1) * pageSize, pageSize);
            }
            pageBean.setList(list);

        } else {
            Long allRow = this.findTotalCount("", null);
            int totalPage = PageBean.countTotalPage(pageSize, allRow.intValue()); // 总页数
            pageBean.setAllRow(allRow.intValue());
            pageBean.setTotalPage(totalPage);
            List<Country> list;
             if (page == 1) {
                list = this.findWithPaging(condition, parameters, "code", page-1, pageSize);
            } else {
                list = this.findWithPaging(condition, parameters, "code", (page - 1) * pageSize, pageSize);
            }
            pageBean.setList(list);
        }
        return pageBean;
    }

    public Country saveInstance(Country country) {

        Country parent = this.findUnique("id=?", new Object[]{country.getTemp4()});
        country.setCode(this.generateCode(parent));
        country.setTemp4(country.getTemp4());

        this.save(country);
        return country;
    }

    /*
     * 生成编码,父级性质是2位编码，子级性质是4位编码
     */
    @SuppressWarnings("unchecked")
    private String generateCode(Country parent) {
        List<Country> groups = null;
//        ?this.findUnique("objid=?", new Object[]{obj_id});
        groups = this.dao.find("temp4=?", new Object[]{parent.getId()}, "id");
        if (groups == null || groups.isEmpty()) {//不存在兄弟
            if (null == parent) {//父亲也为空的话，代表第一次添加
                return "001";
            } else {
                return parent.getCode() + "001";
            }
        }
        String code = "";
        Country max = groups.get(0);
        int maxCode = 0;
        for (Country group : groups) {
            int currCode = Integer.valueOf(group.getCode().substring(group.getCode().length() - 3, group.getCode().length()));
            if (currCode > maxCode) {
                maxCode = currCode;
            }
        }
        if (String.valueOf(max.getCode()).length() == 3) {
            code = valueOf(String.valueOf(maxCode + 1));
        } else {
            code = max.getCode().substring(0, max.getCode().length() - 3) + valueOf(String.valueOf(maxCode + 1));
        }
        return code;
    }

    private String valueOf(String str) {
        if (str == null || str.isEmpty()) {
            return "000";
        }
        try {
            int code = Integer.valueOf(str);
            if (code < 10 && code > 0) {
                return "00" + code;
            }
            if (code < 100 && code >= 10) {
                return "0" + code;
            } else {
                return String.valueOf(code);
            }
        } catch (Exception e) {
            return "000";
        }
    }


    public void deleteById(Integer documentGroupId) {
        Country documentGroup = null;
        documentGroup = this.findUnique("id=?", new Object[]{documentGroupId});

        if (documentGroup != null) {
            String code = documentGroup.getCode();
            String cString = code + "%";

            List<Country> list = this.find("code like ?", new Object[]{cString}, "id");


            for (Country d : list) {
                this.delete(d);
            }
        }
    }

}
